package com.wanmait.baohan.controller.ReimbursementController;

import com.wanmait.baohan.pojo.*;
import com.wanmait.baohan.service.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;


@Controller
@RequestMapping("/reimbursement")
public class ReimbursementDetailsController {
    @Resource
    private BookingTicketApplicationService bookingTicketApplicationService;
    @Resource
    private DictionaryService dictionaryService;
    @Resource
    private ReimbursementService reimbursementService;
    @Resource
    private BookingHotelService bookingHotelService;
    @Resource
    private InvoicePictureService invoicePictureService;
    @Resource
    private TravelApplicationService travelApplicationService;

    /**
     * 周勇
     * 打开费用信息详情
     */
    @RequestMapping("/reimbursementDetails")
    public String reimbursementDetails(Model model, Integer typeId, Integer id,Integer enable) {
        System.out.println("id"+id);
        System.out.println("enable"+enable);
        ReimbursementApplication reimbursementApplication = reimbursementService.selectByIdReimbursement(id,enable);
        Dictionary dictionary = dictionaryService.selectByValue(typeId);
        List<InvoicePicture> invoicePictureList = invoicePictureService.invoiceList(id);
        System.out.println(typeId);
        System.out.println(id);
        if (typeId == 19) {
            //List ticket=bookingCountService.findAllByIdTicket(reimbursementApplication.getStaff().getId(),3);//订票报销
            BookingTicketApplication ticket = bookingTicketApplicationService.selectByReId(id);
            model.addAttribute("details", ticket.getId());//票类信息
           // System.out.println("进入票类");
        }
        if (typeId == 20) {
//            System.out.println("进入差旅");
//            System.out.println("id==========="+id);
            TravelApplication details = travelApplicationService.findById(reimbursementApplication.getTravelApplicationId());
            //System.out.println("application=============="+details.getId());
            model.addAttribute("details", details.getId());
        }
        if (typeId == 38) {
           // System.out.println("进入酒店");
            // List hotels= hotelAccountService.findByIdHotel(reimbursementApplication.getStaff().getId(),3);
            BookingHotelApplication hotels = bookingHotelService.selectByReId(id);
            //System.out.println("酒店信息长度"+hotels.getId());
            model.addAttribute("details", hotels.getId());//酒店信息
            //System.out.println("酒店信息长度"+hotels.getId());
        }
        model.addAttribute("reimbursementApplication", reimbursementApplication);//报销申请
//        System.out.println("报销备注======"+reimbursementApplication.getRemark());
//        System.out.println("申请备注=========="+reimbursementApplication.getRejectRemark());
//        System.out.println("申请表Id"+reimbursementApplication.getTravelApplicationId());
        model.addAttribute("dictionary", dictionary);//报销类型
        if(invoicePictureList.size()>0){
            model.addAttribute("invoicePictureList", invoicePictureList);//报销后的发票图片
        }
        //System.out.println("最后一步");
        return "reimbursement/reimbursementDetails";
    }

    /**
     * 周勇
     * 驳回和通过报销申请
     */
    @RequestMapping("/rejectTheApplication")
    @ResponseBody
    public void rejectTheApplication(HttpServletRequest request, ReimbursementApplication reimbursementApplication) {
        // System.out.println("报销结果========================"+reimbursementApplication.getEnable()+reimbursementApplication.getRejectRemark());
        Staff staff = (Staff) request.getSession().getAttribute("user");
        reimbursementApplication.setUpdateStaff(staff);
        reimbursementApplication.setProcessNodeId(15);

        System.out.println("内容=========="+reimbursementApplication.getRejectRemark());
        System.out.println("id=========="+reimbursementApplication.getUpdateStaff());
        System.out.println("结果========="+reimbursementApplication.getEnable());
        reimbursementApplication.setUpdateTime(new Date());
        //System.out.println(reimbursementApplication.getUpdateTime());
        reimbursementService.reimbursementResult(reimbursementApplication);
        // System.out.println("提交成功");
    }

    /**
     * 周勇
     * 上传图片完成报销
     */

  /*  @RequestMapping("/uploadInvoice")
   public void uploadIvoice(HttpServletRequest request, List<MultipartFile> invoicePic,Integer dictionaryId, Integer reid){
        Staff staff=(Staff)request.getSession().getAttribute("user");
        List<InvoicePicture>invoicePictureList=new IdentityArrayList<>();
        if(invoicePic.size()>0){
            for (MultipartFile f : invoicePic) {
                InvoicePicture invoicePicture=new InvoicePicture();
                    String ext = FilenameUtils.getExtension(f.getOriginalFilename());
                    if (!",jpg,png,gif,bmp,jpeg,webp,".contains("," + ext + ",")) {
                        return;
                    }
                    try {
                        //唯一文件名
                        String fileName = UUID.randomUUID() + "." + ext;
                        //物理路径
                        String path = ResourceUtils.getURL("classpath:").getPath()+"/static/image/reimbursementBill";//request.getServletContext().getRealPath("/static/image");
                        File file = new File(path, fileName);
                        f.transferTo(file);
                        staff.setPhoto(fileName);
                        invoicePicture.setName(fileName);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    invoicePicture.setDictionaryId(dictionaryId);
                    invoicePicture.setReimbursementId(reid);
                    invoicePicture.setStaffId(staff.getId());
                    invoicePictureList.add(invoicePicture);
                }
                    invoicePictureService.addInvoicePic(invoicePictureList);
            }
        }
*/

//    @RequestMapping("/suploadInvoice")
//   public void suploadIvoice(HttpServletRequest request, List<MultipartFile> invoicePic,Integer dictionaryId, Integer reid){
//        Staff staff=(Staff)request.getSession().getAttribute("user");
//        List<InvoicePicture>invoicePictureList =new ArrayList<>();
//        if(invoicePic.size()>0){
//            for (MultipartFile f : invoicePic) {
//                InvoicePicture invoicePicture=new InvoicePicture();
//                    String ext = FilenameUtils.getExtension(f.getOriginalFilename());
//                    if (!",jpg,png,gif,bmp,jpeg,webp,".contains("," + ext + ",")) {
//                        return;
//                    }
//                    try {
//                        //唯一文件名
//                        String fileName = UUID.randomUUID() + "." + ext;
//                        //物理路径
//                        String path = ResourceUtils.getURL("classpath:").getPath()+"/static/image/reimbursementBill";//request.getServletContext().getRealPath("/static/image");
//                        File file = new File(path, fileName);
//                        f.transferTo(file);
//                        staff.setPhoto(fileName);
//                        invoicePicture.setName(fileName);
//                    } catch (IOException e) {
//                        e.printStackTrace();
//                    }
//                    invoicePicture.setDictionaryId(dictionaryId);
//                    invoicePicture.setReimbursementId(reid);
//                    invoicePicture.setStaffId(staff.getId());
//                    invoicePictureList.add(invoicePicture);
//                }
//                    invoicePictureService.addInvoicePic(invoicePictureList);
//            }
//        }


}
